<script setup>
import {ref, onMounted} from 'vue'
import {RouterLink, RouterView} from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'

const userData = ref('')
const isLoading = ref(false)
const error = ref(null)

const fetchUserData = async () => {
  isLoading.value = true;
  error.value = null;
  try {
    const response = await fetch('/api/getEcsReleaseNotes')
    if (!response.ok) {
      throw new Error('Failed to fetch user data')
    }
    const data = await response.text();
    console.log("====data", data)
    userData.value = data
  } catch (err) {
    console.error('Error fetching user data:', err)
    error.value = 'Failed to fetch user data. Please try again.'
  } finally {
    isLoading.value = false
  }
}

onMounted(() => {
  // 可以在这里添加任何需要在组件挂载时执行的逻辑
})
</script>

<template>
  <header>
    <div class="wrapper">
      <HelloWorld/>
      <div class="user-data-section">
        <div class="latest-section" v-if="userData">
          <h3>最新动态</h3>
          <div class="latest-content">
            {{userData}}
          </div>
        </div>
        <p v-if="error" class="error-message">{{ error }}</p>
        <div class="button-container">
          <button @click="fetchUserData" :disabled="isLoading">
            {{ isLoading ? 'Fetching...' : '点击了解最新动态' }}
          </button>
        </div>
      </div>
    </div>
  </header>

  <RouterView/>
</template>

<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
}

.user-data-section {
  display: flex;
  flex-direction: column;
  margin-top: 1rem;
}

.latest-section {
  margin-bottom: 1rem;
}

.latest-section > h3 {
  font-weight: 500;
}

.latest-content {
  font-size: 14px;
  margin-top: 0.5rem;
  background-color: #eff3f8;
  border-radius: 4px;
  padding: 10px;
  line-height: 22px;
}

textarea {
  width: 100%;
  max-width: 500px;
  margin-bottom: 1rem;
}

button {
  padding: 0.5rem 1rem;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #45a049;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.error-message {
  color: red;
  margin-bottom: 1rem;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;
    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>